﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Data;
using System.Web;

namespace Business {
    public class Email {
        CreateConn objcon = new CreateConn();
        public bool SendEmail(string email, string subject, string strMsg) {
            string username = GetUsername(email);
            try {
                SmtpClient client = new SmtpClient();
                client.Host = "webmail.i2iportal.com.au";
                MailMessage message = new MailMessage();
                message.Sender = new MailAddress("do_not_reply@i2iportal.com.au");
                message.From = new MailAddress("do_not_reply@i2iportal.com.au");
                message.To.Add(new MailAddress(email));
                message.Subject = subject;
                //message.Attachments.Add(new Attachment("images/email_header.jpg"));

                message.IsBodyHtml = true;

                string strBody = "<div style=\"background-color: #FFFFFF; margin: auto; padding: 0px; width: 720px;" +
                "font-family: 'Lucida Grande' , 'Lucida Sans Unicode' , 'Lucida Sans Regular' , Tahoma, Verdana, Sans-Serif;\">" +
                "<div style=\"background-color: #50ADE1; height: 105px; margin-bottom: 5px;\">" +
                "<div style=\"background-image: url('http://i2iportal.com.au/images/logo.png'); background-repeat: no-repeat; width: 425px; height: 52px; margin-top:47px; margin-left: 20px; float: left;\" ></div>" +
                "<div style=\"margin-top: 75px; margin-right: 20px; float: right; vertical-align: middle; font-size: small;" +
                "font-weight: bold; color: #FFFFFF; height: 26px;\">G' Day, " + 
                username + "</div></div>" +
                "<div style=\"margin: 20px; padding: 10px; border: solid 1px #d8effc; background-color: #FFFFFF; " + 
                "font-size: medium; font-family:'Lucida Grande' , 'Lucida Sans Unicode' , 'Lucida Sans Regular' , Tahoma, Verdana, Sans-Serif; text-transform: capitalize; color: #1e74c5;\">" +
                subject + "</div><div style=\"padding: 20px; background-color: #FFFFFF; font-size: small; font-family: 'Lucida Grande' , 'Lucida Sans Unicode' , 'Lucida Sans Regular' , Tahoma, Verdana, Sans-Serif;\">" +
                strMsg + "</div><div style=\"vertical-align: middle; margin-top: 20px; text-align: center; background-color: #50ADE1; height: 65px;\">" +
                "<p style=\"margin: 0; font-size: 10px; color: #dff0fa; font-family: 'Lucida Grande' , 'Lucida Sans Unicode' , 'Lucida Sans Regular' , Tahoma, Verdana, Sans-Serif;" +
                "line-height: 18px;\">i2iPortal and the i2iPortal Logo are registered trademarks of i2i-Technologies Company." +
                "<br />PO Box 1192, Cleaveland QLD 4163, Australia Office (07) 3829 1235; Fax (07) 3829 1238; Mobile 0439 717 244<br />" +
                "&copy; <a href=\"http://www.i2i-technologies.com.au\" target=\"_parent\" style=\"color: #093f9c\";>i2i-Technologies</a>" +
                "- <a href=\"http://www.i2iportal.com.au/Unsubscribe.aspx\" target=\"_parent\" style=\"color: #093f9c;" +
                "text-decoration: none;\">unsubscribe</a></p></div></div>";

                //string strBody = "<div style=\"margin: auto; padding: 10px; border: thin solid #36C8EF; width: 600px; font-family: Arial, Helvetica, sans-serif;\">" +
                //"<div style=\"background-color: #CEDEEB; height: 71px; margin-bottom: 5px;\"><img alt=\"i2iPortal logo\" src=\"cid:imageId\" style=\"float: left\"/>" +
                //"<div style=\"padding: 25px 20px 20px 20px; float: right; vertical-align: middle; font-size: small; font-weight: bold; color: #666666; height: 26px;\">G' Day, " +
                //username + "</div></div>" +
                //"<div style=\"padding: 5px 10px 5px 10px; background-color: #666666; font-size: 16pt;font-weight: bold; text-transform: capitalize; color: #FFFFFF;\">" +
                //subject + "</div>" +
                //"<div style=\"padding: 30px 10px 30px 10px; background-color: #F8FCFF; font-size: small;\">" +
                //strMsg + "</div>" +
                //"<div style=\"vertical-align: middle; border-width: 1px; border-style: solid; border-color: #b6bec9; text-align: center; background-color: #CEDEEB; height: 55px;\">" +
                //"<p style=\"margin: 0; font-size: 10px; font-weight: bold; color: #96a2b3; font-family: Arial; line-height: 18px;\">" +
                //"i2iPortal and the i2iPortal Logo are registered trademarks of i2i-Technologies Company.<br />" +
                //"PO Box 1192, Cleaveland QLD 4163, Australia Office (07) 3829 1235; Fax (07) 38291238; Mobile 0439 717 244<br />" +
                //"&copy; <a href=\"http://www.i2i-technologies.com.au\" target=\"_parent\">i2i-Technologies</a> " +
                //"- <a href=\"http://www.i2iportal.com.au/Unsubscribe.aspx\" target=\"_parent\" style=\"color: #808080; text-decoration: none;\">unsubscribe</a></p></div></div>";

                //AlternateView htmlView = AlternateView.CreateAlternateViewFromString(strMsg, null, "text/html");
                //LinkedResource imagelink = new LinkedResource(HttpContext.Current.Server.MapPath(".") + @"\email_header.JPG", "image/jpeg");
                //imagelink.ContentId = "imageId";
                //imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
                //htmlView.LinkedResources.Add(imagelink);

                //message.AlternateViews.Add(htmlView);

                message.Body = strBody;
                client.Send(message);
                return true;
            } catch (Exception err) {
                Console.Write("Err msg:" + err.Message);
                return false;
            }
        }
        public String GetUsername(string email) {
            string user_id = String.Empty;
            string name = String.Empty;
            try {
                string selectQuery = "SELECT user_id, name FROM Users WHERE email = '" + email + "'";
                DataTable dt = new DataTable();
                dt = objcon.SQLConnDataTable(selectQuery);
                user_id = dt.Rows[0]["user_id"].ToString();
                name = dt.Rows[0]["name"].ToString();
            } catch (Exception err) {
                Console.Write("Err msg:" + err.Message);
            }
            if (user_id != String.Empty) {
                return name;
            } else {
                //to retrieve only Filename from the complete path
                int extractPos = email.LastIndexOf("@");
                string username = email.Substring(0, extractPos);
                return username;
            }
        }
    }
}
